//
//  KKBaseAFNetWorking.m
//  AFnetWorking测试
//
//  Created by long on 14-11-18.
//  Copyright (c) 2014年 谭启龙. All rights reserved.
//

#import "XLNetworking.h"

@interface XLNetworking ()
{
    UIActivityIndicatorView * _activityView;
}
@end

@implementation XLNetworking
- (NSString*)getRequestUrl
{
    return self.requestUrl;
}

#pragma mark --普通网络连接

//打印地址用于测试
-(NSString *)showUrlWithDictionary:(NSDictionary *)dic
{
    //打印地址
    NSString * url = [self getRequestUrl];
    for (NSString * key in dic) {
        url = [url stringByAppendingString:[NSString stringWithFormat:@"&%@=",key]];
        url = [url stringByAppendingString:[dic objectForKey:key]];
    }
    //有问号则加问号
    NSNotFound == [url rangeOfString:@"?"].location ? [url stringByAppendingString:@"&"] :false ;
    return url;
}

//请求成功后走的方法用于传递数据
-(void)sucBlockDo:(id)result
{
    //data转json
    if ([result isKindOfClass:[NSData class]]) {
        self.result = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
    }
    else if ([result isKindOfClass:[NSString class]]) {
        NSString * str = (NSString *)result;
        NSData * jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
        self.result = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
    }
    self.originResult = self.result;
    //数据加工处理
    XLBaseNetMode * processresult = [self processResult:self.result];
    if (!processresult) {
        //        self.result = result;
    } else {
//        if ([processresult isKindOfClass:[XLBaseNetMode class]]) {
//            processresult.code = [self.result objectForKey:@"code"];
//            processresult.msg = [self.result objectForKey:@"msg"];
//            processresult.success = [self.result objectForKey:@"msg"];
//            self.result = processresult;
//        }
    }
    //执行块语法
    if (self.sucBlock) {
        self.sucBlock(self.result);
    }
    [self loadFinished];
}
//请求出错的时候走的方法
-(void)errorBlockDo:(id)resulte
{
    NSLog(@"请求出错%@",resulte);
    if (self.errorBlock) {
        self.errorBlock(resulte);
    }
    [self loadFinished];
}

//接受到取消下载的通知

//加载完成后的相关处理
-(void)loadFinished
{
    [_activityView stopAnimating];
}

#pragma mark -- 加载数据的时候缓冲图片
-(void)loadMaskAcivityView:(UIView *)superView
{
    if (superView) {
        _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [_activityView startAnimating];
    }
}

-(void)dealloc
{
    [_activityView stopAnimating];
}

#pragma mark 子类重写的
//对数据的模型化处理
-(id)processResult:(id)result {
    return nil;
}
@end

